package com.shengqin.calculate.service;

import com.alibaba.fastjson2.JSON;
import com.shengqin.calculate.AbstractCalculateScoreComponent;
import com.shengqin.calculate.data.CalculateContext;
import com.shengqin.entity.Standard;
import com.shengqin.entity.SysHealthProject;
import com.shengqin.entity.TestResult;
import com.shengqin.pojo.plan.result.ScoreResult;
import com.shengqin.pojo.plan.result.TestResultKey;
import com.shengqin.service.IStandardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CalculateService extends AbstractCalculateScoreComponent {

    @Resource
    private IStandardService standardService;

    public void execute(CalculateContext context) {
//        log.info("开始初始化计算上下文{}", JSON.toJSONString(context));
        Map<TestResultKey, List<TestResult>> testResultKeyListMap = context.getAllTestResultList().stream().filter(t -> StringUtils.hasLength(t.getPrjName()) && !"身高".equals(t.getPrjName()) && !"体重".equals(t.getPrjName())).collect(Collectors.groupingBy(t -> new TestResultKey(t.getPrjName(), t.getSex(), t.getGradeId().toString())));

        testResultKeyListMap.forEach((resultKey, testResults) -> {
            boolean isGo = true;
            List<Standard> prjStandardList = context.getStandardMap().getOrDefault(resultKey, new ArrayList<>());
            SysHealthProject healthProject = context.getProjectMap().getOrDefault(resultKey.getPrjName() + "_" + resultKey.getGradeId(), null);
            if (CollectionUtils.isEmpty(prjStandardList)) {
                log.error("{}未找到对应的评分标准", JSON.toJSONString(resultKey));
                isGo = false;
            }
            if (ObjectUtils.isEmpty(healthProject)) {
                log.error("{}未找到对应的评分项目", JSON.toJSONString(resultKey));
                isGo = false;
            }
            if (isGo) {
                context.setHealthProject(healthProject);
                context.setPrjTestResultList(testResults);
                if (!CollectionUtils.isEmpty(context.getAllOldTestResultList())) {
                    List<TestResult> currentPrjOldResultList = context.getAllOldTestResultList().stream().filter(t -> t.getPrjName().equalsIgnoreCase(resultKey.getPrjName())).collect(Collectors.toList());
                    context.setOldTestResultList(currentPrjOldResultList);
                }
                try {
                    TestResult testResult = CollectionUtils.isEmpty(context.getPrjTestResultList()) ? null : context.getPrjTestResultList().get(0);
                    TestResult oldTestResult = CollectionUtils.isEmpty(context.getOldTestResultList()) ? null : context.getOldTestResultList().get(0);

                    if (testResult != null) {
                        ScoreResult scoreResult = standardService.buildScoreResult(testResult.getResult(), prjStandardList, healthProject, context.getPlan().getCalculateType(),healthProject.getRadixLength());
                        calculateTestResultScore(testResult, oldTestResult, scoreResult, context);
                    }
                } catch (Exception e) {
                    log.error("执行单个项目{}计算异常", JSON.toJSONString(resultKey), e);
                    throw e;
                }
            }
        });
    }
}
